import requests

def test_get():
    """
    访问百度首页的简单示例
    """
    url = 'https://www.baidu.com'  # 使用HTTPS协议
    
    try:
        # 设置基本的请求头
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        
        # 发送GET请求，设置超时时间
        response = requests.get(url, headers=headers, timeout=10)
        
        # 检查响应状态
        if response.status_code == 200:
            print(f"✅ 访问成功！状态码: {response.status_code}")
            print(f"📄 页面标题: {response.text[response.text.find('<title>')+7:response.text.find('</title>')]}")
            print(f"📏 内容长度: {len(response.text)} 字符")
            
            # 显示部分内容（前200个字符）
            content_preview = response.text[:200].replace('\n', ' ').strip()
            print(f"📖 内容预览: {content_preview}...")
        else:
            print(f"❌ 访问失败！状态码: {response.status_code}")
            
    except requests.exceptions.Timeout:
        print("⏰ 请求超时！请检查网络连接")
    except requests.exceptions.ConnectionError:
        print("🔌 连接错误！请检查网络连接")
    except requests.exceptions.RequestException as e:
        print(f"❗ 请求异常: {str(e)}")
    except Exception as e:
        print(f"💥 其他错误: {str(e)}")

if __name__ == "__main__":
    test_get()